<template>
    <div>
        <div style="padding: 10px;">

            <!--            搜索区域-->
            <div style="margin:10px 0;">
                <el-input v-model="search" placeholder="请输入试卷名称" style="width: 20%;" clearable/>
                <el-button type="primary" style="margin-left: 5px;" @click="load"> 查询</el-button>
                <el-button type="primary" style="margin-left: 5px;" @click="add" v-if="user.role != 'ROLE_STUDENT'"> 添加</el-button>
            </div>
            <el-table
                    :data="tableData"
                    style="width: 100%;"
                    stripe
                    :header-cell-style="{'text-align':'center'}"
                    :cell-style="{'text-align':'center'}"
            >
<!--                <el-table-column type="selection" width="55"></el-table-column>-->
                <el-table-column prop="paperName" label="试卷名称" width="300"></el-table-column>
                <el-table-column prop="status" label="试卷状态" width="140">
                    <template slot-scope="scope">
                        <el-tag type="primary" v-if="scope.row.status === 1">待判分</el-tag>
                        <el-tag type="warning" v-if="scope.row.status === 2">已完成</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="courseName" label="课程名称" width="200"></el-table-column>
                <el-table-column prop="systemScore" label="系统判定得分" width="140"></el-table-column>
                <el-table-column prop="paperScore" label="试卷获得总分" width="140" :formatter="formatScore"></el-table-column>
                <el-table-column prop="fullScore" label="试卷满分"></el-table-column>
                <el-table-column prop="createTime" label="考试时间" :formatter="formatDate" width="200"></el-table-column>
                <!--操作-->
<!--                <el-table-column label="操作">-->
<!--                    <template #default="scope">-->
<!--                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)"-->
<!--                        ><i class="el-icon-edit-outline"></i> 编辑-->
<!--                        </el-button>-->

<!--                        <el-popconfirm-->
<!--                                class="ml-5"-->
<!--                                confirm-button-text='确定'-->
<!--                                cancel-button-text='我再想想'-->
<!--                                icon="el-icon-info"-->
<!--                                icon-color="red"-->
<!--                                title="您确定删除吗？"-->
<!--                                @confirm="handleDelete(scope.$index,scope.row)"-->
<!--                        >-->
<!--                            <el-button type="danger" size="small" slot="reference"><i-->
<!--                                    class="el-icon-remove-outline"></i> 删除-->
<!--                            </el-button>-->
<!--                        </el-popconfirm>-->

<!--                    </template>-->
<!--                </el-table-column>-->
            </el-table>
            <!--            分页栏-->
            <div style="margin: 10px 0;">
                <el-pagination
                        v-model:pageNum="pageNum"
                        :page-sizes="[5, 10, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                >
                </el-pagination>
            </div>

        </div>
    </div>
</template>

<script>

    import request from "../utils/request";

    export default {
        name: 'Score',
        components: {},
        data() {
            return {
                form: {},
                roles: {},
                user: {},
                dialogVisible: false,
                search: '',
                pageSize: 5,
                pageNum: 1,
                total: 20,
                tableData: [],
                college:[],
                status: [{name: "启用", value: "1"}, {name: "禁用", value: "0"}],
                rules: {
                    scoreId: [
                        {required: true, message: '请输入用戶ID', trigger: 'blur'},
                        {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ]
                }
            }
        },
        created() {
            this.getUser();
            this.load()
        },
        methods: {
            load() {
                this.pageNum = 1;
                // get方法需要params，post不要 "http://localhost:9090/score"
                request.get("/examInfo/selectScore", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        paperName: this.search,
                        studentId:"3120182109222"
                    }
                }).then(res => {
                    console.log("res:", res);
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                })

                //请求学院数据
                request.get("/college/findAll").then(res => {
                    console.log("res:", res);
                    this.college = res.data
                })
            },
            getUser() {
                this.user = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
                if (!this.user) {
                    this.$message.info("获取用户失败，请重新登录")
                }
            },
            add() {
                this.dialogVisible = true;
                this.form = {};
                this.form.useStatus = "1"; //初始化使用状态
            },
            save() {
                console.log("form:", this.form)
                if (this.form.id) { // 更新
                    request.post('/score/save', this.form).then(res => {
                        console.log("code:", res.code)
                        if (res.code == '200') {
                            this.$message.success("保存成功")
                        } else {
                            this.$message.error("保存失败")
                        }
                        this.load() //刷新表格
                        this.dialogVisible = false
                        console.log("success")
                    })
                } else { //新增
                    request.post("/score/save", this.form).then(res => {
                        console.log(res.data)
                        if (res.code == '200') {
                            this.$message.success("添加成功")
                        } else {
                            this.$message.error("添加失败")
                        }
                        this.load() //刷新表格
                        this.dialogVisible = false
                        console.log("success")
                    })
                }

            },
            handleEdit(index, row) {
                this.form = JSON.parse(JSON.stringify(row))
                this.dialogVisible = true
                console.log(row)
            },
            handleDelete(index, row) {
                console.log(row.id)
                //注意url/{路径}/
                this.request.delete("/score/delete/" + row.id).then(res => {
                    console.log(res)
                    if (res.data === true) {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })

            },
            handleSizeChange(pageSize) { //改变当前每页的个数触发
                this.pageSize = pageSize
                console.log(pageSize)
                this.load()
            },
            handleCurrentChange(pageNum) {  //改变当前页码触发
                this.pageNum = pageNum
                this.load()
            },
            formatDate(row, column) {
                // 获取单元格数据
                let data = row[column.property]
                if(data == null) {
                    return null
                }
                let dt = new Date(data)
                return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
            },
            formatScore(row, column) {
                // 获取单元格数据
                if(row.status == 1){
                    return '待批改'
                }else{
                    return row.paperScore
                }
            },


        }

    }
</script>
